package com.lcredu.spring;

import com.lcredu.spring.annotation.After;
import com.lcredu.spring.annotation.Before;
import com.lcredu.spring.component.SmartAnimalAspect;

import java.lang.reflect.Method;

/**
 * @author 黎长容
 * @date 2025-07-16 22:12
 */
public class LcrTest {
    public static void main(String[] args) throws Exception {
        //获取这个切面类
        Class<SmartAnimalAspect> smartAnimalAspectClass = SmartAnimalAspect.class;
        //遍历该类的所有方法
        for (Method declaredMethod : smartAnimalAspectClass.getDeclaredMethods()) {
            //如果这个方法上是Before
            if (declaredMethod.isAnnotationPresent(Before.class)) {
                System.out.println("m:= " + declaredMethod.getName());
                //获取Before注解
                Before annotation = declaredMethod.getAnnotation(Before.class);
                //得到Before的value值
                System.out.println("value:= " + annotation.value());
                //得到切入要执行的方法. 
                Method declaredMethod1 =
                        smartAnimalAspectClass.getDeclaredMethod(declaredMethod.getName());
                //调用切入方法
                declaredMethod1.invoke(smartAnimalAspectClass.newInstance(), null);
            } else if (declaredMethod.isAnnotationPresent(After.class)) {
                System.out.println("m:= " + declaredMethod.getName());
                After annotation = declaredMethod.getAnnotation(After.class);
                System.out.println("value:= " + annotation.value());
            //得到切入要执行的方法
                Method declaredMethod1 =
                        smartAnimalAspectClass.getDeclaredMethod(declaredMethod.getName());
//调用切入方法
                declaredMethod1.invoke(smartAnimalAspectClass.newInstance(), null);            }
            
        }
    }
}
